<template>
  <div>
    <el-drawer
      :visible.sync="isDrawer"
      :direction="direction"
      :size="this.$store.state.user.userIsPhone ? '90%' : '30%'"
      with-header="false"
    >
      <div class="head-box">
        <div class="identifier-box"></div>
        <div>附件信息</div>
      </div>
      <div class="anne-box">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-success="uploadSuccess"
          multiple
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </div>
      <div class="head-box">
        <div class="identifier-box"></div>
        <div class="milestone-title">里程碑信息</div>
      </div>
      <el-button
        size="small"
        type="primary"
        @click="addStageItem"
        style="margin-left: 40px"
        >点击新增</el-button
      >
      <div class="milestone-box">
        <div
          class="milestone-item"
          v-for="(item, index) in milestoneData"
          :key="index"
        >
          <div class="milestone-name">
            <input type="text" v-model="item.milestonename" />
          </div>
          <div class="milestone-time">
            <el-date-picker
              v-model="item.milestonetime"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
          <div class="del-identifier" @click="delStageItem(index)">
            <i class="iconfont icon-shanchu"></i>
          </div>
        </div>
      </div>
      <div class="head-box" style="height: 0"></div>
      <div class="sub-btn">
        <div @click="sunmitMilestone">提交</div>
        <div @click="resetting">取消</div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  getProFileListApi,
  getProjectMilestonesApi,
  updateMilestoneApi,
  delProFileApi,
} from "@/api/index";

export default {
  props: {
    drawer: {
      type: Boolean,
      default: false,
    },
    projectRow: {
      type: Object,
      default: false,
    },
  },
  data() {
    return {
      // 项目编号
      proCode: "",
      // 是否弹出抽屉
      isDrawer: false,
      // 抽屉弹出方向
      direction: "rtl",
      uploadUrl: "apis/api/FileUpload",
      // 文件列表
      fileList: [],
      // 折叠栏
      activeName: 0,
      // 阶段源数据
      stageOption: [
        {
          proStage: "SalesResearch",
          stageName: "售前调研",
        },
        {
          proStage: "Develop",
          stageName: "研发阶段",
        },
        {
          proStage: "ElectricImplement",
          stageName: "电气实施",
        },
        {
          proStage: "Design",
          stageName: "产品设计",
        },
        {
          proStage: "DebugTogether",
          stageName: "联调阶段",
        },
        {
          proStage: "Waiting4Sign",
          stageName: "等待验收",
        },
        {
          proStage: "Finish",
          stageName: "已完成",
        },
      ],
      originMilles: [],
      // 里程碑
      milestoneData: [],
      // 里程碑时间
      milestoneTime: "",
    };
  },

  mounted() {},

  methods: {
    openDrawer(row) {
      this.proName = row.proshortname;
      this.proCode = row.procode;
      this.isDrawer = true;
      this.getProFile(row.procode);
      this.getProMilestones(row.procode);
      this.uploadUrl = `apis/api/FileUpload?procode=${row.procode}&empcode=${this.$store.state.user.userCode}`;
    },

    // 获取当前项目的文件列表
    async getProFile(code) {
      let data = {
        procode: code,
      };
      let res = await getProFileListApi(data);
      this.fileList = [];
      res.forEach((item) => {
        this.fileList.push({
          name: item.filename,
          url: item.guid,
          guid: item.guid,
        });
      });
    },
    handleRemove(file, fileList) {
      this.delProFile(file.guid);
    },
    handlePreview(file) {},
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    // 文件上传成功
    uploadSuccess(response, file, fileList) {},
    // 文件删除之前
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 删除文件
    async delProFile(id) {
      let data = {
        guid: id,
      };
      let res = await delProFileApi(data);
    },

    // 获取当前项目的里程碑
    async getProMilestones(code) {
      this.originMilles = [];
      let data = {
        procode: code,
      };
      let res = await getProjectMilestonesApi(data);
      this.originMilles = [...res];
      this.milestoneData = res;
    },

    // 删除当前的里程碑
    delStageItem(index) {
      this.milestoneData.splice(index, 1);
    },

    // 新增里程碑的阶段
    addStageItem() {
      this.milestoneData.push({
        id: null,
        procode: this.proCode,
        milestonename: "",
        milestonetime: "",
      });
    },
    // 提交修改
    async sunmitMilestone() {
      this.milestoneData.forEach((item) => {
        console.log("取消修改----->", !item.milestonename, !item.milestonetime);
        if (!item.milestonename || !item.milestonetime) {
          this.$message.warning("请将里程碑信息填写完整！！！");
          return;
        }
      });
      let data = [...this.milestoneData];
      let query = {
        procode: this.proCode,
        empcode: this.$store.state.user.userCode,
      };
      let res = await updateMilestoneApi(data, query);
      console.log("确认修改----->", res);
    },

    resetting() {
      this.milestoneData = [];
      this.milestoneData = [...this.originMilles];
      console.log("取消修改----->", this.milestoneData, this.originMilles);
    },
  },
};
</script>

<style lang="scss" scoped>
.main-box {
  .head-box {
    border-bottom: 1px solid #dddcdc;
    height: 30px;
    line-height: 30px;
    text-align: start;
    font-size: 13px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding-left: 20px;
    display: flex;
    align-items: center;

    .identifier-box {
      height: 10px;
      width: 5px;
      margin-right: 6px;
      background: #11a70c;
    }
  }

  .anne-box {
    /* border: 1px solid red; */
    width: 100%;
    padding-left: 8%;
    display: flex;

    .upload-demo {
      /* border: 1px solid red; */
      width: 100%;
      text-align: start;

      .upload-btn {
        background: rgba($color: #000000, $alpha: 0);
        color: #000000;
        border: none;
      }

      > ul::-webkit-scrollbar {
        width: 4px;
        height: 8px;
      }
      > ul::-webkit-scrollbar-thumb {
        border-radius: 10px;
        -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        opacity: 0.2;
        background: fade(rgb(78, 182, 214), 60%);
      }
      > ul::-webkit-scrollbar-track {
        -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        border-radius: 0;
        background: fade(rgb(78, 182, 214), 30%);
      }
    }
  }

  .milestone-box {
    /* border: 1px solid red; */
    width: 100%;
    max-height: 400px;
    padding-left: 8%;
    overflow: auto;

    .drawer-title {
      font-size: 16px;
    }

    .milestone-item {
      display: flex;

      > input {
        border: 1px solid #dddcdc;
      }

      .milestone-name,
      .milestone-time {
        /* border: 1px solid red; */
        height: 36px;
        line-height: 35px;
        margin-top: 15px 0;

        > input {
          height: 28px;
          width: 120px;
          text-align: start;
          border: 1px solid #dddcdc;
          margin-right: 5px;
          /* border: none; */
          border-radius: 4px;
        }
      }

      .del-identifier {
        /* border: 1px solid red; */
        height: 36px;
        width: 36px;
        padding-left: 5px;
        line-height: 36px;
      }
      .del-identifier:hover {
        cursor: pointer;
      }
    }

    .item-box {
      /* border: 1px solid green; */
      width: 100%;
      display: flex;

      .del-box {
        margin-left: 5px;
      }

      .name-box {
        width: 120px;
        text-align: end;
        padding-right: 5px;
      }

      .time-box {
        width: 180px;
      }

      .name-box,
      .time-box {
        /* border: 1px solid blue; */
        height: 30px;
        font-size: 14px;
        border: none;
      }
    }

    .add-mil-box:hover {
      cursor: pointer;
    }
  }

  .sub-btn {
    /* border: 1px solid #dddcdc; */
    width: 100%;
    margin: 10px 0;
    display: flex;
    justify-content: end;
    padding-right: 10px;

    > div {
      /* border: 1px solid #ee1a1a; */
      text-align: center;
      height: 32px;
      width: 20%;
      margin-left: 10px;
      line-height: 32px;
      font-size: 13px;
      color: #fff;
      background: #57a6e7;
      border-radius: 5px;
    }

    > div:hover {
      cursor: pointer;
      background: rgba($color: #57a6e7, $alpha: 0.8);
    }
  }
}

.milestone-box::-webkit-scrollbar {
  width: 4px;
}
.milestone-box::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  opacity: 0.2;
  background: fade(rgb(78, 182, 214), 60%);
}
.milestone-box::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: fade(rgb(78, 182, 214), 30%);
}

@media screen and (max-width: 768px) {
  .main-box {
    background: #ee0909;
  }
}
</style>